﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using QRCodeBluetooth.BUS;
using QRCodeBluetooth.Models;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace QRCodeBluetooth.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AppController : ControllerBase
    {
        private JwtSettings _jwtSettings;
        IHttpContextAccessor _access;
        public AppController(IOptions<JwtSettings> jwtSettingsAccesser, IHttpContextAccessor access
        )
        {
            _jwtSettings = jwtSettingsAccesser.Value;
            _access = access;
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        [HttpPost]
        [Route("OwnerSubmit")]
        public ResponseMessage OwnerSubmit(OwnerInfo ownerInfo)
        {
            ResponseMessage message = new ResponseMessage();
            OwnerBUS ownerBUS = new OwnerBUS();
            message = ownerBUS.AddOwner(ownerInfo);
            if (message.errcode == 0)
            {
                string token = MakeToken(ownerInfo);
                if (token.Equals(""))
                {
                    message.errcode = 1; message.errmsg = "生成Token错误"; message.data = null;
                    return message;
                }
                else
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = token;
                }
            }

            return message;
        }

        private string MakeToken(OwnerInfo ownerInfo)
        {
            string strToken = "";
            var claim = new Claim[]{
                   new Claim(ClaimTypes.Name,ownerInfo.ownerName),
                   new Claim("IdentityInf",ownerInfo.identityInf.ToString())
            };
            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSettings.SecretKey));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var token = new JwtSecurityToken
            (
                issuer: _jwtSettings.Issuer,
                audience: _jwtSettings.Audience,
                claims: claim,
                notBefore: DateTime.Now,
                expires: DateTime.Now.AddMinutes(118),//过期时间                
                signingCredentials: creds
            );
            try
            {
                strToken = new JwtSecurityTokenHandler().WriteToken(token);
            }
            catch (Exception ee) { return ""; }

            return strToken;
        }

        /// <summary>
        /// 查询审核记录
        /// </summary>
        [HttpPost]
        [Route("QueryAudit")]
        public ResponseMessage QueryAudit(string str_IdentityInf)
        {
            ResponseMessage message = new ResponseMessage();
            AppBUS appBUS = new AppBUS();
            AppAuditRecord appAuditRecord = new AppAuditRecord();
            appAuditRecord.identityInf = str_IdentityInf;
            message = appBUS.QueryAppAuditRecord(appAuditRecord);
            return message;
        }

        /// <summary>
        /// 查询区域
        /// </summary>
        [HttpPost]
        [Route("QueryAreaInfo")]
        public ResponseMessage QueryAreaInfo(ProjectInfo projectInfo)
        {
            ResponseMessage message = new ResponseMessage();
            ProjectBUS projectBUS = new ProjectBUS();
            message = projectBUS.QueryProject(projectInfo);
            return message;
        }

        /// <summary>
        /// 查询乘梯区域信息
        /// </summary>
        [HttpPost]
        [Route("QueryAreaByOwner")]
        public ResponseMessage QueryAreaByOwner(string str_IdentityInf)
        {
            ResponseMessage message = new ResponseMessage();
            AppBUS appBUS = new AppBUS();
            message = appBUS.QueryAreaByOwner(str_IdentityInf);
            return message;
        }

        /// <summary>
        /// 查询乘梯设备
        /// </summary>
        [HttpPost]
        [Route("QueryDevice")]
        public ResponseMessage QueryDevice(AppVoucher appVoucher)
        {
            ResponseMessage message = new ResponseMessage();
            AppBUS appBUS = new AppBUS();
            message = appBUS.QueryDevice(appVoucher);
            return message;
        }

        /// <summary>
        /// 查询乘梯楼层
        /// </summary>
        [HttpPost]
        [Route("QueryVoucher")]
        public ResponseMessage QueryVoucher(AppVoucher appVoucher)
        {
            ResponseMessage message = new ResponseMessage();
            AppBUS appBUS = new AppBUS();
            message = appBUS.QueryVoucher(appVoucher);
            return message;
        }

        /// <summary>
        /// 提交申请
        /// </summary>
        [HttpPost]
        [Route("AddRecordInfo")]
        public ResponseMessage AddRecordInfo(AppAuditRecord appAuditRecord)
        {
            ResponseMessage message = new ResponseMessage();
            RecordBUS recordBUS = new RecordBUS();
            message = recordBUS.AddRecord(appAuditRecord);
            return message;
        }

    }
}
